package com.sdhs.ssp.transcation.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.common.entity.PaginationEntity;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;
import com.sdhs.paas.sdk.core.web.client.PaasRestTemplate;
import com.sdhs.ssp.transcation.bean.AppInfoBean;
import com.sdhs.ssp.transcation.dto.AppInfoDTO;
import com.sdhs.ssp.transcation.dto.req.AppInfoReqDTO;
import com.sdhs.ssp.transcation.dto.res.AppInfoResDTO;
import com.sdhs.ssp.transcation.service.AppInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * @author AppInfo
 * @version 1.0
 * @Title: 代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-09-18
 */

@RefreshableRestController
@RequestMapping(value = "/appInfo")
@Api("测试案例")
public class AppInfoController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private AppInfoService appInfoService;
	
	@Autowired
	@Qualifier("externalRestTemplate")
	private PaasRestTemplate externalRestTemplate;  
	
	/**
	 * 分页查询接口
	 */
	@PostMapping("/getAppInfoPageList")
	@ApiOperation("分页查询")
	public PaasBaseResponse<AppInfoResDTO> getAppInfoPageList(
			@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		PageInfoDTO pageInfoDTO = appInfoReqDTO.getBody().getPageInfo();

		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setPageSize(pageInfoDTO.getPageSize());

		AppInfoBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);

		PaginationEntity<AppInfoBean> page = appInfoService.getPageAppInfoListNew(pageRequest, itimsBusinessBean);
		PageInfoDTO resPageInfoDTO = new PageInfoDTO();
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();

		resPageInfoDTO.setRowCount(page.getTotal());
		appInfoResDTO.setPageInfo(resPageInfoDTO);
		List<AppInfoBean> appInfoBeanList = page.getRows();
		List<AppInfoDTO> dtoList = new ArrayList<AppInfoDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(appInfoBeanList, new TypeReference<List<AppInfoDTO>>() {
		});
		appInfoResDTO.setAppInfoDTOList(dtoList);
		return new PaasBaseResponse<>(appInfoResDTO);
	}

	/**
	 * 保存接口
	 */
	@PostMapping("/saveAppInfo")
	@ApiOperation("保存接口")
	public PaasBaseResponse<AppInfoResDTO> saveAppInfo(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		appInfoBean.setId(uuid);
		appInfoBean.setIsDeleted(0);
		Boolean flag = appInfoService.saveAppInfo(appInfoBean);
		appInfoDTO.setId(uuid);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		appInfoResDTO.setAppInfoDTO(appInfoDTO);
		return new PaasBaseResponse<>(appInfoResDTO);
	}

	/**
	 * 修改接口
	 */
	@PostMapping("/updateAppInfo")
	@ApiOperation("修改接口")
	public PaasBaseResponse<AppInfoResDTO> updateAppInfo(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		Boolean flag = appInfoService.updateAppInfo(appInfoBean);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		appInfoResDTO.setAppInfoDTO(appInfoDTO);
		return new PaasBaseResponse<>(appInfoResDTO);
	}

	/**
	 * 获取详情接口
	 */
	@PostMapping("/getAppInfoDetail")
	@ApiOperation("获取详情接口")
	public PaasBaseResponse<AppInfoResDTO> getAppInfoDetail(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		appInfoBean = appInfoService.getAppInfoDetail(appInfoBean.getId());
		appInfoDTO = EntityDtoConvertUtil.convertToDTO(appInfoBean, AppInfoDTO.class);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		appInfoResDTO.setAppInfoDTO(appInfoDTO);
		return new PaasBaseResponse<>(appInfoResDTO);
	}

	/**
	 * 删除接口
	 */
	@PostMapping("/deleteAppInfo")
	@ApiOperation("删除接口")
	public PaasBaseResponse<AppInfoResDTO> deleteAppInfo(@RequestBody PaasBaseRequest<AppInfoReqDTO> appInfoReqDTO) {
		AppInfoDTO appInfoDTO = appInfoReqDTO.getBody().getAppInfoDTO();
		AppInfoBean appInfoBean = EntityDtoConvertUtil.convertToEntity(appInfoDTO, AppInfoBean.class);
		Boolean flag = appInfoService.deleteAppInfo(appInfoBean);
		AppInfoResDTO appInfoResDTO = new AppInfoResDTO();
		return new PaasBaseResponse<>(appInfoResDTO);
	}
	
	/**
	 * 测试接口
	 * */
	@GetMapping("/hello")
	@ApiOperation("测试接口")
	public String sayHell(){
		logger.info("hello info");
		logger.debug("hello debug");
		logger.error("hello error");
		return "hello world";
	}
	
	/**
	 * resttemplate使用案例
	 * */
	@GetMapping("/rest")
	@ApiOperation("Get请求案例")
	public String testRest(){
		//resttemplate get请求案例
		ResponseEntity<String> res = externalRestTemplate.getForEntity("http://127.0.0.1:9087/appInfo/hello", String.class);
		
		return res.getBody();
	}
	
	@PostMapping("/postrest")
	@ApiOperation("Post请求案例")
	public String testRestPost(){
		//post请求案例
		HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("key1", "values");
        AppInfoReqDTO appInfoReq = new AppInfoReqDTO();
        AppInfoDTO appInfoDTO = new AppInfoDTO();
        appInfoDTO.setId("bb0bcabb5cdc4031a7dc4e44b7884ba0");
        appInfoReq.setAppInfoDTO(appInfoDTO);
		ResponseEntity<PaasBaseResponse<AppInfoResDTO>> result = externalRestTemplate.exchange(
				"http://127.0.0.1:9087/appInfo/getAppInfoDetail", HttpMethod.POST,
				new HttpEntity<>(new PaasBaseRequest<>(appInfoReq),requestHeaders),
				new ParameterizedTypeReference<PaasBaseResponse<AppInfoResDTO>>() {
				});
		System.out.println(result.getBody());
		return "success";
	}
}
